import Vue from 'vue'
let commonUtils = {}

// 为空返回true
commonUtils.emptyValid = (v) => {
	// console.log(typeof(v))
	switch (typeof(v)) {
		case 'string':
			// console.log('String')
			if (v.length <= 0 || v === 'null' || v === 'undefined') {
				return true
			} else {
				return false
			}
			case 'number':
				// console.log('Number')
				if (isNaN(v) || v == Infinity) {
					return true
				} else {
					return false
				}
				case 'undefined':
					// console.log('undefined')
					return true
				case 'object':
					// console.log('Object')
					if (v == null) {
						return true
					} else if (v.length <= 0) {
						return true
					} else if (JSON.stringify(v) == '{}') {
						return true
					} else {
						return false
					}
					case 'function':
						// console.log('Function')
						return false
					default:
						// console.log('其他')
						return true
	}

}

commonUtils.isJSON = (str) => {
	if (typeof str == 'string') {
		try {
			var obj = JSON.parse(str);
			if (typeof obj == 'object' && obj) {
				return true;
			} else {
				return false;
			}

		} catch (e) {
			return false;
		}
	} else {
		return false
	}
}
commonUtils.valueFormat = (value, rules) => {
	if (rules) {
		value = rules(value);
	}
	if (commonUtils.emptyValid(value)) {
		return '-';
	} else {
		return value.toString();
	}
}

/**
 * 替换所有匹配exp的字符串为指定字符串
 * @param exp 被替换部分的正则
 * @param newStr 替换成的字符串
 */
commonUtils.replaceAll = function(exp, newStr, result) {
	return result.replace(new RegExp(exp, "gm"), newStr);
};

/**
 * 原型：字符串格式化
 * @param args 格式化参数值
 */
commonUtils.format = function(str, ...args) {
	let result = str;
	if (arguments.length < 1) {
		return result;
	}

	args.forEach((value, index) => {
		if (undefined != value) {
			result = commonUtils.replaceAll("\\{" + index + "\\}", value, result);
		}
	}); // 如果模板参数是数组
	// console.log(arguments);
	// if (arguments.length == 1 && typeof(args) == "object") {
	// 	// 如果模板参数是对象
	// 	data = args;
	// }
	// for (var key in data) {
	// 	var value = data[key];
	// 	if (undefined != value) {
	// 		result = commonUtils.replaceAll("\\{" + key + "\\}", value, result);
	// 	}
	// }
	return result;
}


//用户权限判断
commonUtils.isAuthority = (authority, ignore = true) => {
	let userInfo = Vue.prototype.$tools.getUserInfo();
	if(Vue.prototype.$tools.emptyValid(userInfo)){
		return
	}
	if (ignore && (userInfo.userName == 'admin' || userInfo.roleKey == 'admin')) {
		return true;
	}
	if (userInfo.permsStr) {
		typeof userInfo.permsStr == 'string' ? userInfo.permsStr = userInfo.permsStr.split(',') : null;
		if (userInfo.permsStr.indexOf(authority) >= 0) {
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}

/**
 * toLocaleString 国际化的使用
 * @param num 需转换数字
 * @param point 保留位数
 * @param isMoney 是否转换金额符号
 * @returns
 */
commonUtils.numberFormat = (num, point, isMoney = false) => {
	let lan = Vue.prototype.$tools.getLan(); //获取当前语言
	if (lan == "zh_CN") {
		lan = 'zh'
	} else if (lan == "en_US") {
		lan = 'en-US'
	} else {
		lan = 'pt-BR'
	}
	//数字千位符展示
	const numberOptions = {
		minimumFractionDigits: point
	};
	const numberFormatHub = new Intl.NumberFormat(lan, numberOptions);

	//货币千位符展示，只取巴西的货币符
	const currencyOptions = {
		style: 'currency',
		currency: 'BRL'
	};
	const currencyBRLHub = new Intl.NumberFormat(lan, currencyOptions);
	//国际化千位符的使用,保留两位小数
	if (isMoney) {
		return currencyBRLHub.format(num);
	} else {
		if (num == null || num == undefined || num.length <= 0) {
			return '';
		}
		var tempNum = Number(num);
		if (tempNum.toString() == "NaN") {
			return num;
		}
		num = tempNum.toFixed(point);
		return numberFormatHub.format(num);
	}
}

commonUtils.getRadomInt = (count = 4) => {
	//创建空数组
	var temp = [];
	//for循环来生成随机数组元素，
	for (var i = 0; i < parseInt(count); i++) {
		temp.push(parseInt(Math.random() * 10))
	}
	console.log(temp, temp.length, temp.join(''));
	return temp.join('')
}

// 获取当前日期时间
commonUtils.getTime = (isHms = false) => {
	let date = new Date();
	// 日
	let getDate = date.getDate() < 10 ? `0` + date.getDate() : date.getDate();
	// 月
	let month =
		date.getMonth() + 1 < 10 ?
		`0` + (date.getMonth() + 1) :
		date.getMonth();
	// 年
	let year = date.getFullYear();
	// 时
	let hours = date.getHours() < 10 ? `0` + date.getHours() : date.getHours();
	//分
	let minutes = date.getMinutes() < 10 ? `0` + date.getMinutes() : date.getMinutes();
	//秒
	let second = date.getSeconds() < 10 ? `0` + date.getSeconds() : date.getSeconds();
	
	if(isHms){
		return `${year}-${month}-${getDate} ${hours}:${minutes}:${second}`;
	}else{
		return `${year}-${month}-${getDate}`;
	}
}

//去除时间后面的时分秒
commonUtils.removeDateMAS = (time,lx='MAS') => {
	if (Vue.prototype.$tools.emptyValid(time)) {
		return ''
	}
	if(lx=='MAS'){
		let tmpTime = time.split(' ')
		return tmpTime[0]
	}
	if(lx=='S'){
		let tmpTime = time.substring(0,time.length-3)
		return tmpTime
	}
}

export default commonUtils
